<template>
  <div class="divBox">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <div class="container">
          <el-form inline>
            <el-form-item label="创建时间">
              <el-date-picker
                v-model="tableFrom.dateRange"
                style="width: 240px"
                value-format="yyyy-MM-dd HH:mm:ss"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                :default-time="['00:00:00', '23:59:59']"
              ></el-date-picker>
            </el-form-item>
            <el-form-item label="用户">
              <el-input
                v-model="tableFrom.homeName"
                :disabled="isCreate==2"
                placeholder="请输入"
                class="selWidth"
                clearable
              />
            </el-form-item>

            <el-form-item label="消息类型：">
              <el-select
                v-model="tableFrom.status"
                placeholder="请选择"
                clearable
                style="width: 320px"
              >
                <el-option
                  v-for="dict in dict.type.sys_normal_disable"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>

            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            <el-button type="primary" icon="el-icon-search" @click="getList(1)">搜索</el-button>
          </el-form>
        </div>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain size="mini" @click="readListfun">已读清单</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="add"
              v-hasPermi="['finance:expenseControl:add']"
            >新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleEdit('2')"
              v-hasPermi="['finance:expenseControl:view']"
            >查看</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleEdit('1')"
              v-hasPermi="['finance:expenseControl:edit']"
            >修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['finance:expenseControl:edit']"
            >删除</el-button>
          </el-col>

          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <!-- <el-button size="mini" type="primary" @click="add">新增</el-button> -->
      </div>
      <el-table
        v-loading="listLoading"
        :data="tableData.data"
        @row-click="oneLine"
        size="mini"
        ref="multipleTable"
        :header-cell-style=" {fontWeight:'bold'}"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <!-- <el-table-column label="序号"  type="index" /> -->
        <!-- <el-table-column prop="sort" label="排序"/> -->

        <el-table-column prop="homeAddress" label="标题" />
        <el-table-column prop="homeAddress" label="消息内容" />
        <el-table-column prop="homeAddress" label="消息类型" />
        <el-table-column prop="homeAddress" label="用户名" />
        <el-table-column prop="homeAddress" label="姓名" />
        <el-table-column prop="homeAddress" label="创建时间" />
        <el-table-column prop="homeAddress" label="已读时间" />
        <el-table-column prop="homeAddress" label="推送渠道" />

        <!-- 
          <el-table-column label="操作" min-width="200" fixed="right" align="center">
            <template slot-scope="scope">
              <el-button type="text" size="small" @click="handleEdit(scope.row,'1')">编辑</el-button>
              <el-button type="text" size="small" @click="handleDelete(scope.row.id, scope.$index)">删除</el-button>
              <el-button type="text" size="small" @click="handleEdit(scope.row,'2')" class="mr10">查看</el-button>
            </template>
        </el-table-column>-->
      </el-table>

      <div class="block mb20">
        <el-pagination
          :page-sizes="[10, 20, 30, 40]"
          :page-size="tableFrom.limit"
          :current-page="tableFrom.page"
          layout="total, sizes, prev, pager, next, jumper"
          :total="tableData.total"
          @size-change="handleSizeChange"
          @current-change="pageChange"
        />
      </div>
    </el-card>

    <el-dialog
      :title="isCreate===0 ? '添加' : '编辑/查看'"
      :visible.sync="dialogVisible"
      width="80%"
      :before-close="handleClose"
      v-if="dialogVisible"
    >
      <div v-loading="loading">
        <restaurantDialog
          ref="restaurantDialog"
          :isCreate="isCreate"
          :categoryOptions="categoryOptions"
          :selectItem="selectItem"
          @getList="getList(1)"
          @close="handleClose"
        ></restaurantDialog>
      </div>
    </el-dialog>

    <el-dialog
      title="已读清单"
      :visible.sync="readListPup"
      width="80%"
      :before-close="handleClose"
      v-if="readListPup"
    >
      <readList @close="handleClose"></readList>
    </el-dialog>
  </div>
</template>
    
    <script>
import {
  listType,
  getType,
  delType,
  addType,
  updateType,
  refreshCache
} from "@/api/system/dict/type";
import readList from "./com/readList.vue";
import restaurantDialog from "./com/restaurantDialog";
import { homeGetList, homeGetDelete } from "@/api/village";
export default {
  name: "advertisingSpaceManage",
  components: { restaurantDialog, readList },
  dicts: ["sys_normal_disable"],
  data() {
    return {
      readListPup: false,
      status: 1,
      tableDatas: [
        {
          date: "2016-05-02",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1518 弄"
        },
        {
          date: "2016-05-04",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1517 弄"
        },
        {
          date: "2016-05-01",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1519 弄"
        },
        {
          date: "2016-05-03",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1516 弄"
        }
      ],
      multiple: true,
      showSearch: true,
      single: true,
      time: undefined,
      dialogVisible: false,
      isCreate: 0,
      selectItem: undefined,
      listLoading: true,
      tableData: {
        data: [],
        total: 0
      },
      tableFrom: {
        page: 1,
        limit: 20,
        name: undefined, //名称
        isShow: undefined //抢购状态
      },
      loading: false,
      categoryOptions: [],
      ids: []
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    readListfun() {
      this.readListPup = true;
    },
    resetQuery() {
      this.$refs.queryForm.resetFields();
      this.handleQuery();
    },
    oneLine(e) {
      console.log(e);
    },
    handleSelectionChange(selection) {
      // this.ids = [];

      // if (list.length > 1) {
      //   this.$refs.table.clearSelection();
      //   this.$refs.table.toggleRowSelection(
      //     list[list.length - 1]
      //   );
      // }
      // this.ids = [list[list.length - 1]];
      // this.ids =  this.ids.map(item => item.roleId)
      // // console.log(this.ids,'this.ids')
      // return;
      // ?console.log(selection,'selectionselection')
      this.ids = selection.map(item => item.roleId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
      console.log(this.ids, "idsids");
    },
    // 删除
    handleDelete(id, idx) {
      this.$modal
        .confirm("确定要删除吗？")
        .then(() => {
          this.loading = true;
          return delModel(modelIds);
        })
        .then(() => {
          this.loading = false;
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .finally(() => {
          this.loading = false;
        });
      // let data={
      //   id:id
      // }
      this.$modalSure().then(() => {
        homeGetDelete({ id: id }).then(() => {
          this.$message.success("删除成功");
          this.tableData.data.splice(idx, 1);
        });
      });
    },

    // 编辑
    handleEdit(type, row) {
      if (type == 1) {
        this.isCreate = 1;
      } else if (type == 2) {
        this.isCreate = 2;
      }
      this.selectItem = row;

      this.dialogVisible = true;
    },

    // 列表
    getList(num) {
      this.listLoading = true;
      this.tableFrom.page = num ? num : this.tableFrom.page;
      listType(this.tableFrom)
        .then(res => {
          this.tableData.data = res.rows;
          this.tableData.total = res.total;
          this.tableData.data.map(item => this.$set(item, "isEdit", false));
          this.listLoading = false;
        })
        .catch(res => {
          this.listLoading = false;
        });
    },
    pageChange(page) {
      this.tableFrom.page = page;
      this.getList();
    },
    handleSizeChange(val) {
      this.tableFrom.limit = val;
      this.getList();
    },
    handleStatusChange(id, name) {
      if (name === "上架") {
        postCateringStatus({ ids: id, isShow: 1 }).then(() => {
          this.getList(1);
        });
      }

      if (name === "下架") {
        postCateringStatus({ ids: id, isShow: 0 }).then(() => {
          this.getList(1);
        });
      }
    },
    add() {
      this.selectItem = undefined;
      this.isCreate = 0;
      this.dialogVisible = true;
    },
    handleClose() {
      this.readListPup = false;
      this.dialogVisible = false;
    },

    //跳转套餐
    handleView(id) {
      this.$router.push(`/family/restaurantManage2?id=${id}`);
    }
  }
};
</script>
    
    <style scoped>
</style>
    